package com.zdl.interestingapp.view.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zdl.interestingapp.R;
import com.zdl.interestingapp.data.entity.MessageLogging;

import java.util.List;

/**
 * @author zdl
 * @date 2018/4/23 16:13
 * email zdl328465042@163.com
 * description
 */
public class QARecycleViewAdapter extends RecyclerView.Adapter<QARecycleViewAdapter.InnerViewHolder> {

    private List<MessageLogging> data;

    public QARecycleViewAdapter(List<MessageLogging> data) {
        this.data = data;
    }

    @Override
    public int getItemViewType(int position) {
        MessageLogging messageLogging = data.get(position);
        if (messageLogging.getFlag() == 1){
            return 1;
        }else {
            return 2;
        }
    }

    @NonNull
    @Override
    public InnerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        if (viewType == 1){
            return new InnerViewHolder(inflater.inflate(R.layout.module_recycle_item_qa_q, parent, false));
        } else {
            return new InnerViewHolder(inflater.inflate(R.layout.module_recycle_item_qa_a, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull InnerViewHolder holder, int position) {
        MessageLogging messageLogging = data.get(position);
        if (null == messageLogging){
            return;
        }

        if (messageLogging.getFlag() == 1){
            holder.tv_q.setText(messageLogging.getContent());
        }else {
            holder.tv_a.setText(messageLogging.getContent());
        }
    }

    @Override
    public int getItemCount() {
        return null == data ? 0 : data.size();
    }

    class InnerViewHolder extends RecyclerView.ViewHolder {

        TextView tv_q, tv_a;

        InnerViewHolder(View itemView) {
            super(itemView);

            tv_q = itemView.findViewById(R.id.tv_q);
            tv_a = itemView.findViewById(R.id.tv_a);
        }
    }
}
